/*
 * Copyright 2012-2021 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.autoconfigure.jms;

import java.time.Duration;

import javax.jms.ConnectionFactory;
import javax.jms.ExceptionListener;

import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.destination.DestinationResolver;
import org.springframework.transaction.jta.JtaTransactionManager;
import org.springframework.util.Assert;

/**
 * 使用合理的默认值配置 {@link DefaultJmsListenerContainerFactory}。
 *
 * @author Stephane Nicoll
 * @author Eddú Meléndez
 * @since 1.3.3
 */
public final class DefaultJmsListenerContainerFactoryConfigurer {

	private DestinationResolver destinationResolver;

	private MessageConverter messageConverter;

	private ExceptionListener exceptionListener;

	private JtaTransactionManager transactionManager;

	private JmsProperties jmsProperties;

	/**
	 * 设置要使用的 {@link DestinationResolver}，如果默认情况下不应将任何目标解析器与工厂关联，则设置为 {@code null}。
	 * @param destinationResolver {@link DestinationResolver}
	 */
	void setDestinationResolver(DestinationResolver destinationResolver) {
		this.destinationResolver = destinationResolver;
	}

	/**
	 * 设置要使用的 {@link MessageConverter}，如果应使用开箱即用的转换器，则设置为 {@code null}。
	 * @param messageConverter {@link MessageConverter}
	 */
	void setMessageConverter(MessageConverter messageConverter) {
		this.messageConverter = messageConverter;
	}

	/**
	 * 设置要使用的 {@link ExceptionListener}，如果默认情况下不应关联任何异常监听器，则设置为 {@code null}。
	 * @param exceptionListener {@link ExceptionListener}
	 */
	void setExceptionListener(ExceptionListener exceptionListener) {
		this.exceptionListener = exceptionListener;
	}

	/**
	 * 设置要使用的 {@link JtaTransactionManager}，如果应禁用 JTA 支持，则设置为 {@code null}。
	 * @param transactionManager {@link JtaTransactionManager}
	 */
	void setTransactionManager(JtaTransactionManager transactionManager) {
		this.transactionManager = transactionManager;
	}

	/**
	 * 设置要使用的 {@link JmsProperties}。
	 * @param jmsProperties {@link JmsProperties}
	 */
	void setJmsProperties(JmsProperties jmsProperties) {
		this.jmsProperties = jmsProperties;
	}

	/**
	 * 配置指定的 jms 监听器容器工厂。该工厂可以进一步调整并覆盖默认设置。
	 * @param factory 要配置的 {@link DefaultJmsListenerContainerFactory} 实例
	 * @param connectionFactory 要使用的 {@link ConnectionFactory}
	 */
	public void configure(DefaultJmsListenerContainerFactory factory, ConnectionFactory connectionFactory) {
		Assert.notNull(factory, "Factory must not be null");
		Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
		factory.setConnectionFactory(connectionFactory);
		factory.setPubSubDomain(this.jmsProperties.isPubSubDomain());
		if (this.transactionManager != null) {
			factory.setTransactionManager(this.transactionManager);
		}
		else {
			factory.setSessionTransacted(true);
		}
		if (this.destinationResolver != null) {
			factory.setDestinationResolver(this.destinationResolver);
		}
		if (this.messageConverter != null) {
			factory.setMessageConverter(this.messageConverter);
		}
		if (this.exceptionListener != null) {
			factory.setExceptionListener(this.exceptionListener);
		}
		JmsProperties.Listener listener = this.jmsProperties.getListener();
		factory.setAutoStartup(listener.isAutoStartup());
		if (listener.getAcknowledgeMode() != null) {
			factory.setSessionAcknowledgeMode(listener.getAcknowledgeMode().getMode());
		}
		String concurrency = listener.formatConcurrency();
		if (concurrency != null) {
			factory.setConcurrency(concurrency);
		}
		Duration receiveTimeout = listener.getReceiveTimeout();
		if (receiveTimeout != null) {
			factory.setReceiveTimeout(receiveTimeout.toMillis());
		}
	}

}
